function setTab(name,cursel,n){
         for(var i=1; i<=n; i++){
          var tab=document.getElementById(name+i);
          var con=document.getElementById("con_"+name+"_"+i);
          if (tab!=null){
        	  tab.className=i==cursel?"hover":"";  
          }
          if (con!=null){
        	  con.style.display=i==cursel?"block":"none";  
          }
         }
        }

/**
 * 删除地址
 * @param addressId
 * @return
 */
function deleteAddress(addressId,isMyCountFlag){
	if(confirm("确认删除该地址？")){
		$.ajax({
			   cache : false,
			   type: "POST",
			   url: "/neworder/deleteAddress.jspa?addressId="+addressId,
			   dataType : 'json',
			   success:function(json){
			   		if(json.flag == 1){
			   			if(isMyCountFlag==1){//充值送发票
						  window.location.href='/personalCenter/recharge.jspa';
						  return;
			   			}
			   			initAddress();
			   			window.location.href='/neworder/writeOrder.jspa?target=1';
			   		}else{
			   			alert(json.msg);
			   		}
			   }
			})
	}
}

/**
 * 初始化地址信息
 * @return
 */
function initAddress(){
	$.ajax({
		   type: "POST",
		   url: "/neworder/initAddress.jspa",
		   dataType : 'json',
		   success:function(json){
		   		if(json.infoDTO.flag == 1){
		   			if(json.addressList.length==0){
		   				clearAddressInfo();
		   				$("#table_address").html("");
		   				setTab('myorder',2,4);
		   			}else{
		   				var html = getAdressInfoHtml(json);
			   			$("#table_address").html(html);
			   			getAmount();
		   			}
		   		}else{
		   			alert(json.infoDTO.msg);
		   		}
		   }
		})
}

function initYmCoupon(couponType,inputObjectId){
	  var userId=$.cookie("login_username");
	  if(userId==null||userId==""){
		  return;
	  }
	$.ajax({
		   type: "POST",
		   url: "/neworder/initYmCoupon.jspa?couponType="+couponType,
		   dataType : 'json',
		   async : false,
		   success:function(json){
				if(json && json.length>0){
					if(couponType=="COUPON_TYPE_DISCOUNT"){
			              var couponListDiv="<ul><li><span>折扣券号</span><b>优惠</b></li>";	             
					}else{
						var couponListDiv = "<ul><li><span>也买券号</span><b>面值</b></li>";
					}
					 for(var i=0;i<json.length;i++){
		                 var couponInfo= json[i];
		                 if(couponType=='COUPON_TYPE_VOUCHERS' && couponInfo.bindsGoodList!=undefined && couponInfo.bindsGoodList.length>0){
		                	 couponListDiv+="<li onmousedown=\"getValue('"+inputObjectId+"','"+couponInfo.couponCode+"');showAndHide('List_zhekou','hide'); if($.browser.msie){$('#tip1').css('z-index','1');$('#payexpress_alt').hide();}\"><a href=\"#\"><i>"+couponInfo.couponCode+"</i><ins>提货券</ins></a></li>";
		                 }else{
		                	 couponListDiv+="<li onmousedown=\"getValue('"+inputObjectId+"','"+couponInfo.couponCode+"');showAndHide('List_zhekou','hide'); if($.browser.msie){$('#tip1').css('z-index','1');$('#payexpress_alt').hide();}\"><a href=\"#\"><i>"+couponInfo.couponCode+"</i><ins>"+couponInfo.favAmount+"</ins></a></li>";
		                 }
		                }
		             couponListDiv+="</ul>";
		             $("#List_zhekou").html(couponListDiv);
		             showAndHide('List_zhekou','show'); 
		             if($.browser.msie){
		            	 $('#tip1').css('z-index','900');             
		            	 $('#payexpress_alt').hide();
		            }
		    		 $("#couponSize").text(json.length);
				}
		   	}
		});
}
/**
 * 初始化折扣券数量
 * @param couponType
 */
function initYmCouponSize(couponType){
var userId=$.cookie("login_username");
if(userId==null||userId==""){
	  return;
}
$.ajax({
	   type: "POST",
	   url: "/neworder/initYmCoupon.jspa?couponType="+couponType,
	   dataType : 'json',
	   async : false,
	   success:function(json){
		if(json && json.length>0){
			 $("#couponSize").text(json.length);
		}
	   }
})
}
/**
 * 生成html地址信息
 * @param json
 * @return
 */
function getAdressInfoHtml(json){
	var html = ""
	for(var i=0; i < json.addressList.length;i++){
		var address = json.addressList[i];
		html += "<tr><td width='3%' align='center' valign='middle'><label>";
		html += " <input name='addressId' type='radio' id='addressId'";
		if(json.defaultAddressId == address.addressId){
			html += "checked='checked'";
		}
		html +=	" value='"+address.addressId+"' onclick='changeAddress("+address.addressId+")'/>";
		html += " </label></td>";
		html += " <td width='8%' align='left' valign='middle'>"+address.name+"</td>";
		html += " <td width='41%' align='left' valign='middle'>"+address.province+"&nbsp;"+address.city+"&nbsp;"+address.district+"&nbsp;"+address.address+"</td>";
		if(address.mobile == null || address.mobile == ''){
			html += "<td width='16%' align='center' valign='middle'>"+address.phone+"</td>";
		}else{
			html += "<td width='16%' align='center' valign='middle'>"+address.mobile+"</td>";
		}
		html += "<td width='16%' align='center' valign='middle'>"+address.zipCode+"</td>";
		html += ' <td width=\'16%\' align=\'center\' valign=\'middle\'>[ <a href=\'javascript:window.location.href="#con_myorder_1";clickUpdate('+address.addressId+')\'>编辑</a> ] [ <a href="javascript:deleteAddress('+address.addressId+');">删除</a> ]</td></tr>';
	}
	return html;
}

/**
 * 添加地址
 * @return
 */
function addAddress(isMyCountFlag){
	var options = { 
			dataType : "json",	
			type : "post",
			async:false,
			success: function(json) { 
			  if(json.flag == 1){
				 if(isMyCountFlag==1){//充值送发票
					  window.location.href='/personalCenter/recharge.jspa';
					  return;
				 }
				 initAddress();
				 window.location.href='/neworder/writeOrder.jspa';
			  }else{
				  alert(json.msg);
			  }
			}
		};
	
	var third = $.trim($("#third").val());
	
	var phone1 = $.trim($("#phone1").val());
	var phone2 = $.trim($("#phone2").val());
	var phone = getPhone();
	var isAll = validateAddress(third,phone);
	if(isAll){
		$('#addressForm').attr('action', '/neworder/addAddress.jspa?memberAddress.areaId='+third+"&memberAddress.phone="+phone);
		$("#addressForm").ajaxSubmit(options);
	}
}

/**
 * 验证地址信息
 * @return
 */
function validateAddress(third,phone){
	
	var name = $.trim($("#name").val());
	if(name == null || name == ""){
		alert("请输入收货人信息！");
		$("#name").focus();
		return false;
	}
	if(name.length > 25){
		alert("您输入的收货人信息过长！");
		$("#name").focus();
		return false;
	}
	
	if(third == null || third == ""){
		alert("请选择区域！");
		return false;
	}
	
	var address = $.trim($("#address").val());
	if(address == null || address == "" || address == "请输入具体地址"){
		alert("请输入地址信息！");
		$("#address").focus();
		return false;
	}
	if(address.length < 4){
		alert("您输入的地址信息过短！");
		$("#address").focus();
		return false;
	}
	if(address.length > 100){
		alert("您输入的地址信息过长！");
		$("#address").focus();
		return false;
	}
	
	var mobile = $.trim($("#mobile").val());
	
	if((mobile == null || mobile == '') && (phone == null || phone == '')){
		alert("手机和电话必须选填一项！");
		$("#mobile").focus();
		return false;
	}
	
	if(mobile != null && mobile != '' && !isMobile(mobile)){
		alert("请输入正确的手机号码！");
		$("#mobile").focus();
		return false;
	}
	
	if(phone != null && phone != '' && !isCityPhoneNumber(phone)){
		alert("请输入正确的电话号码！");
		$("#phone2").focus();
		return false;
	}
	
	var zipCode = $.trim($("#zipCode").val());
	if(zipCode == null || zipCode == ""){
		alert("请输入您的邮编！");
		$("#zipCode").focus();
		return false;
	}
	if(!isPost(zipCode)){
		alert("请输入正确的邮编信息！");
		$("#zipCode").focus();
		return false;
	}
	return true;
}

/**
 * 获取电话号码
 * @return
 */
function getPhone(){
	var phone1 = $.trim($("#phone1").val());
	var phone2 = $.trim($("#phone2").val());
	var phone = null;
	if(phone1 != null && phone1 != "" && phone2 != null && phone2 != ""){
		phone = phone1+"-"+phone2;
	}else if(phone1 != null && phone1 != "" && (phone2 == null || phone2 == "")){
		phone = phone1;
	}else if((phone1 == null || phone1 == "") && phone2 != null && phone2 != ""){
		phone = phone2;
	}
	return phone;
}

/**
 * 点击更新地址按钮
 * @return
 */
function clickUpdate(addressId,isMyCountFlag){
	setTab('myorder',2,4);
	$('#addressHref').attr('href', "javascript:updateAddress("+addressId+","+isMyCountFlag+")");
	
	$.ajax({
		   type: "POST",
		   url: "/neworder/getAddress.jspa?addressId="+addressId,
		   dataType : 'json',
		   success:function(json){
		   		if(json.infoDTO.flag == 1){
		   			fillAddressInfo(json);
		   		}else{
		   			alert(json.infoDTO.msg);
		   		}
		   }
		});
}

/**
 * 点击添加地址按钮
 * @return
 */
function clickAdd(){
	$('#addressHref').attr('href', 'javascript:addAddress();');
	clearAddressInfo();
}

/**
 * 清空地址信息
 * @return
 */
function clearAddressInfo(){
	 $("#name").val('');
	 $("#address").val('');
	 $("#mobile").val('');
	 $("#zipCode").val('');
	 $("#zipCode").val('');
	 $("#phone1").val('');
	 $("#phone2").val('');
	 area.changeByThirdId(-1);
}

/**
 * 填充地址信息
 * @param json
 * @return
 */
function fillAddressInfo(json){
	 area.changeByThirdId(json.memberAddress.areaId);
	 $("#name").val(json.memberAddress.name);
	 $("#address").val(json.memberAddress.address);
	 $("#mobile").val(json.memberAddress.mobile);
	 $("#zipCode").val(json.memberAddress.zipCode);
	 var phone = json.memberAddress.phone;
	 var phones = phone.split("-");
	 var phone1 = '';
	 var phone2 = '';
	 if(phones.length > 1){
		 phone1 = phones[0];
		 phone2 = phones[1];
	 }else{
		 phone2 = phones[1];
	 }
	 $("#phone1").val(phone1);
	 $("#phone2").val(phone2);
}

/**
 * 更新地址信息
 * @param addressId
 * @return
 */
function updateAddress(addressId,isMyCountFlag){
	var options = { 
			dataType : "json",	
			type : "post",
			async:false,
			success: function(json) { 
			  if(json.flag == 1){
				 if(isMyCountFlag==1){//充值送发票
					  window.location.href='/personalCenter/recharge.jspa';
					  return;
				 }
				 initAddress();
				 window.location.href='/neworder/writeOrder.jspa';
			  }else{
				  alert(json.msg);
			  }
			}
		};
	
	var third = $.trim($("#third").val());
	
	var phone1 = $.trim($("#phone1").val());
	var phone2 = $.trim($("#phone2").val());
	var phone = getPhone();
	var isAll = validateAddress(third,phone);
	
	if(isAll){
		$('#addressForm').attr('action', '/neworder/updateAddress.jspa?memberAddress.areaId='+third+"&memberAddress.phone="+phone+"&memberAddress.addressId="+addressId);
		$("#addressForm").ajaxSubmit(options);
	}
}

function validateInvoiceInfo(invoiceTypeId){
	
	var invoiceType = getRadioCheckedValue("writeOrderParaDTO.invoiceType");
	
	if(invoiceTypeId == 'orderInvoice.invoiceType'){
		 invoiceType = getRadioCheckedValue(invoiceTypeId);
	}else{
		 invoiceType = getRadioCheckedValue("writeOrderParaDTO.invoiceType");
	}
	if(invoiceType == 0){ //普通发票
		var title = $.trim($("#title").val());
		if(title == ''){
			alert("您选择的为普通发票，请填写发票抬头！");
			return false;
		}
		if(title.length > 100){
			alert('对不起，您输入的发票抬头过长！');
			return false;
		}
	}else if(invoiceType == 1){  //增值税发票
		var companyName = $.trim($("#companyName").val());
		if(companyName == ''){
			alert('您选择的为增值税发票，请填写公司名称！');
			return false;
		}
		if(companyName.length > 50){
			alert('对不起，您输入的公司名称过长！');
			return false;
		}	
		
		var companyAddr = $.trim($("#companyAddr").val());
		if(companyAddr == ''){
			alert('您选择的为增值税发票，请填写寄送地址！');
			return false;
		}
		if(companyAddr.length > 100){
			alert('对不起，您输入的寄送地址过长！');
			return false;
		}
		
		var companyPhone = $.trim($("#companyPhone").val());
		if(companyPhone == ''){
			alert('您选择的为增值税发票，请填写公司电话！');
			return false;
		}
		if(!isCityPhoneNumber(companyPhone)){
			alert('您选择的为增值税发票，公司电话请填写正确的格式！如：021-61838383');
			return false;
		}
		
		var bankName = $.trim($("#bankName").val());
		if(bankName == ''){
			alert('您选择的为增值税发票，请填写开户行信息！');
			return false;
		}
		if(bankName.length > 50){
			alert('对不起，您填写的开户行信息过长！');
			return false;
		}
		
		var bankNo = $.trim($("#bankNo").val());
		if(bankNo == ''){
			alert('您选择的为增值税发票，请填写账号信息！');
			return false;
		}
		if(bankNo.length > 30){
			alert('对不起，您填写的账号信息过长！');
			return false;
		}
		if(!isNumber(bankNo)){
			alert('您选择的为增值税发票，账号信息请填写数字！');
			return false;
		}
		var companyRateNo = $.trim($("#companyRateNo").val());
		if(companyRateNo == ''){
			alert('您选择的为增值税发票，请填写税务号信息！');
			return false;
		}
		if(companyRateNo.length > 30){
			alert('对不起，您填写的税务号信息过长！');
			return false;
		}
		if(!isNumber(companyRateNo)){
			alert('您选择的为增值税发票，税务号信息请填写数字！');
			return false;
		}
	}
	
	return true;
}

/**
 * 去支付页面按钮
 * @return
 */
function toBuy(){
	var addressEditCss = $("#con_myorder_2").css("display");
	if(addressEditCss == "block"){
		alert("请完善地址信息");
		return;
	}
	var addressId = getRadioCheckedValue("addressId"); 
	
	if(!validateInvoiceInfo()){
		return;
	}
	
	var sendRequire = $.trim($("#sendRequire").val());
	if(sendRequire=="（请输入您的留言，最多20字）"){
		var a = document.getElementById("sendRequire");
		a.innerHTML="";
	}
	if(sendRequire.length > 20){
		alert('输入的附加说明不能超过20个字！');
		return;
	}
	if(textValidate(sendRequire)){
		alert('您输入的附加说明包含特殊字符，请重新输入！');
		return;
	}
	
	var options = { 
			dataType : "json",	
			type : "post",
			async:false,
			success: function(json) { 
			if(json){	
			  if(json.flag == 1){
				  //window.location.href="/neworder/choosePayWay.jsp"
				  window.location.replace("/neworder/choosePayWay.jspa");
			  }else if(json.flag == -1){ //0元订单
				  var tokenName = $("input[name='struts.token.name']").val();
		    	  var tokenValue = $("input[name='struts.token']").val();
				  //window.location.href='/neworder/confirmOrder.jspa?payType=PAY_TYPE_DOWN&struts.token.name='+tokenName+"&struts.token="+tokenValue;
		    	  window.location.replace('/neworder/confirmOrder.jspa?payType=PAY_TYPE_DOWN&struts.token.name='+tokenName+"&struts.token="+tokenValue);
			  }else{
				  alert(json.msg);
			  }
			}
			}
		};
	$('#orderForm').attr('action', '/neworder/goToChoosePayWay.jspa?writeOrderParaDTO.addressId='+addressId);
	$("#orderForm").ajaxSubmit(options);
}

/**
 * 判断是否有特殊字符
 * @param str
 * @return
 */
function textValidate(str) { 
	
	var txt = new RegExp("[\\<\\>\"]"); 
	if (txt.test(str)) { 
		return true;
	}
	return false;
} 

/**
 * 获取金额信息
 */
function getAmount(){
	$.ajax({
		   type: "POST",
		   url: "/neworder/getAmount.jspa",
		   dataType : 'json',
		   success:function(json){
		   		if(json.infoDTO.flag == 1){
		   			$("#goodsAmount").html(json.goodsAmount);
		   			$("#disCountAmount").html(json.disCountAmount);
		   			$("#scoreAmount").html(json.scoreAmount);
		   			$("#sendAmount").html(json.sendAmount);
		   			$("#ymAmount").html(json.ymAmount);
		   			$("#orderAmount").html(json.orderAmount);
		   			$("#ymSaveAmount").html(json.ymAmount);
		   			$("#scoreSaveAmount").html(json.scoreAmount);
		   			$("#rechargeAmount").html(json.rechargeAmount);
		   			$("#cardAmount").html(json.cardAmount);
		   			$("#restAccountBalance").html(json.restAccountAmount);
		   		}else{
		   			alert(json.infoDTO.msg);
		   		}
		   }
		})
}

/**
 * 使用也买券
 * @return
 */
function useYmCoupon(){
	var ymCouponCode = $.trim($("#ymCouponCode").val());
	if(ymCouponCode == '请输入也买券号' || ymCouponCode == ''){
		alert("请输入也买券号码");
		return;
	}
	if(ymCouponCode.length > 40){
		alert("对不起，您输入的也买券账号过长！");
		return;
	}
	$.ajax({
		   cache : false,
		   type: "POST",
		   url: "/neworder/useYmCoupon.jspa?ymCouponCode="+ymCouponCode,
		   dataType : 'json',
		   success:function(json){
		   		if(json.infoDTO.flag == 1){
		   			alert(json.infoDTO.msg);
		   			getUseCardList();
		   			getAmount();
		   			
		   		}else{
		   			alert(json.infoDTO.msg);
		   		}
		   }
		})
}

/**
 * 使用积分
 * @return
 */
function useScore(){
	var score = $.trim($("#score").val());
	if(score == '' || score == '请输入您要抵用的积分数'){
		alert("请输入要兑换的积分数！");
		return ;
	}
	if(!isNumber(score)){
		alert("对不起，所输入的积分需要是数字！");
		return ;
	}
	if(score == 0){
		alert("对不起，所输入的积分需要是大于零的正整数，且为100的倍数！");
		return;
	}
	//add by lijiheng
	if(score>9999999999){
		alert("积分输入过大，最大10位数字！");
		return;
	}
	if(score%100 != 0){
		alert("对不起，所输入的积分需要是100的倍数，请重新输入！");
		return;
	}
	$.ajax({
		   cache : false,
		   type: "POST",
		   url: "/neworder/useScore.jspa?score="+score,
		   dataType : 'json',
		   success:function(json){
		   		if(json.infoDTO.flag == 1){
		   			alert(json.infoDTO.msg);
		   			getUseCardList();
		   			getAmount();
		   		}else{
		   			alert(json.infoDTO.msg);
		   		}
		   }
		})
}

function useRecharge(){
	var rechargePassword = $("#rechargePassword").val();
	var rechargeAmount = $.trim($("#recharge").val());
	if(rechargeAmount == '' || rechargeAmount == '请输入您要抵扣的金额'){
		alert('请输入要抵扣的金额 ！');
		return ;
	}else{
		if(!isNumber(rechargeAmount) || !isInteger(rechargeAmount) || rechargeAmount <= 0 ){
			alert("也买账户所要抵扣的金额必须为正整数 ！");
			return;
		}
		if(rechargeAmount.length > 8){
			alert("对不起，您输入要抵扣的金额过长，请重新输入 ！");
			return;
		}
		if(rechargePassword == ''){
			alert("请输入也买账户支付密码 ！");
			return ;
		}
	}
	$.ajax({
		   cache : false,
		   type: "POST",
		   url: "/neworder/useRecharge.jspa?rechargePassword="+rechargePassword+"&rechargeAmount="+rechargeAmount,
		   dataType : 'json',
		   success:function(json){
				
		   		if(json.infoDTO.flag == 1){
		   			alert(json.infoDTO.msg);
		   			getUseCardList();
		   			getAmount();
		   		}else{
		   			alert(json.infoDTO.msg);
		   		}
		   }
		})
}

/**
 * 用户选择用户地址操作
 * @param addressId
 * @return
 */
function changeAddress(addressId){
	if (addressId + "" == "-9999"){
		$("#send_date_container :radio:first").click();
		$("#invoice_type_container :radio:first").click();
		
		$("#send_date_container").hide();
		$("#invoice_type_container").hide();
	} else {
		$("#send_date_container").show();
		$("#invoice_type_container").show();
	}
	
	$.ajax({
		   cache : false,
		   type: "POST",
		   url: "/neworder/changeAddress.jspa?addressId="+addressId,
		   dataType : 'json',
		   success:function(json){
		   		if(json.flag == 1){
		   			getUseCardList();
		   			getAmount();
		   		}else{
		   			alert(json.msg);
		   		}
		   }
		})
}

/**
 * 生成订单操作
 * @return
 */
function toAddOrder(){
	var obj = document.getElementById("submitJs");
	if(obj.value==1){
		return;
	}
	
	obj.value=1;
	var payType = getRadioCheckedValue("payType");
	
	if(payType == null || payType == ''){     
		alert("请选择支付方式！");   
		return ;
	}
	$('#payWayForm').submit();
	
}

//忘记密码方法
function forgotPassword(){
             $.ajax({
            	       cache : false,
                       type: "POST",
                       url: "/personalCenter/forgotPasswordForPay.jspa",
                       dataType:'json',
                       data:"",
                       success: function(json){
                           if(json.flag == '0'){
                              showsub_cc();
                           }
                           else{
                              showsub_aa();
                               var tempMobile = json.msg;
                               var mobille = tempMobile.substring(0,3) + "****"+ tempMobile.substring(7);
                               document.getElementById("isBindIngMobile").innerHTML=mobille;
                           }
                       }
                    });  
}


//发送密码方法
function sendSmsByPayPwd(){
                 $.ajax({
                	   cache : false,
                       type: "POST",
                       url: "/personalCenter/sendSmsByPayPwd.jspa",
                       dataType:'json',
                       data:"",
                       success: function(json){
                           if(json.flag == '0'){
                               document.getElementById("retrievePwd").innerHTML="系统错误，请联系客服!";
                           }
                           else{
                               showsub_bb();
                               var tempMobile = json.rechargeAccountDTO.bindIngMobile.toString();
                               var mobille = tempMobile.substring(0,3) + "****"+ tempMobile.substring(7);
                               document.getElementById("retrievePwd").innerHTML=mobille;
                           }
                       }
                    });  
                }
                
function toBinding(){
	 window.open("/personalCenter/showVerifyIndex.jspa","","");
}

/**
 * 设置支付密码
 * @return
 */
function setPwd(){
    window.location.href="/personalCenter/recharge.jspa?comeUrl2="+window.location.href;
}

function getOrderProcess(){
	try{
		$.ajax( {
			cache : false,
			type : "get",
			url : "/neworder/getOrderProccessInfo4Ajax.jspa",
			dataType : 'json',
			success : function(json) {
				try{
					if (json){
						if(json.flag==2){
							alert(json.msg);
							window.location.href='/';
						}else if(json.flag==3){
							window.location.href=json.msg;
						}
					}
				}catch(e2){}
			}
		});
	}catch(e1){}
}

function toggleCardBox(){
	$("#card_box_div").slideToggle('fast', function(){
		if ($("#card_box_div").css("display") == "block"){
		$("#card_box_trigger").html("（-） 使用礼品卡");
		}else{
			$("#card_box_trigger").html("（+） 使用礼品卡");
	}
	});		
}

function showBindCardLayer(trigger){	
	
	var triggerOffset = $(trigger).offset();
	
	var top = triggerOffset.top + $(trigger).outerHeight();
	var left = triggerOffset.left;

	// iPad position fix
	if (/iPad/i.test(navigator.userAgent)) {
		top -= $(window).scrollTop();
	}
	
	$("#bind_card_layer").hide();
	
	$("#bind_card_list tr:gt(0)").remove();
	
	$("#bind_card_layer").css({left: left+'px', top: top+'px', visibility : 'hidden', height: 'auto', width: '460px'}).show();
	
	var w = $("#bind_card_layer").width();
	var h = $("#bind_card_layer").height();
	
	$("#bind_card_layer").hide().css({visibility : 'visible', height: '0px', width: '0px'});
	
	$("#bind_card_layer").animate({width: w + 'px', height: h + 'px'}, function(){$("#bind_card_layer").css({height: 'auto'});});
	
	getBindCardList();	
}

function hideBindCardLayer(){
	$("#bind_card_layer").animate({width: '0px', height: '0px'}, function(){$("#bind_card_layer").hide();});
}

function delUseCard(cardNo){
	$.ajax({
		   cache : false,
		   type: "POST",
		   url: "/neworder/delUseCard.jspa",
		   data: {cardNo: cardNo},
		   dataType : 'json',
		   success:function(json){
		   		if(json.flag == 1){		   			
		   			getUseCardList();
		   			getAmount();
		   		}else{
		   			alert(json.msg);
		   		}
		   }
		});
}

function useCard(){
	
	var cardNo = $("#card_no_input").val();
	var cardPassword = $("#card_password_input").val();
	
	if(cardNo == ""){
		alert("卡号不能为空！");
		return ;
	}
	if(cardPassword == ""){
		alert("密码不能为空！");
		return ;
	}
		
	$.ajax({
		   cache : false,
		   type: "POST",
		   url: "/neworder/useCard.jspa",
		   data: {cardNo: cardNo, cardPassword: cardPassword},
		   dataType : 'json',
		   success:function(json){
		   		if(json.flag == 1){
		   			
		   			$("#card_no_input").val("");
		   			$("#card_password_input").val("");
		   			
		   			getUseCardList();
		   			getAmount();
		   		}else{
		   			alert(json.msg);
		   		}
		   }
		});
}

function useBindCard(){
	var data = "";
	
	$("#bind_card_list input:checked").each(function (i) {
		if (data != ""){
			data += "&";
		}
		data += "bindCardNos=" + encodeURIComponent($(this).val());
	});
	
	if (data == ""){
		alert("请至少选择一张礼品卡！");
		return ;
	}
		
	$.ajax({
		   cache : false,
		   type: "POST",
		   url: "/neworder/useBindCard.jspa",
		   data: data,
		   dataType : 'json',
		   success:function(json){
		   		if(json.flag == 1){		   			
		   			getUseCardList();
		   			getAmount();
		   			
		   			hideBindCardLayer();
		   		}else{
		   			alert(json.msg);
		   		}
		   }
		});
}

function getUseCardList(){
	$.ajax( {
		cache : false,
		type : "get",
		url : "/neworder/getUseCardList.jspa",
		dataType : 'json',
		success : function(json) {			
			if (json){
				if(json.flag==0){
					alert(json.msg);
				}else{					
					$("#use_card_list tr:gt(0)").remove();
					
					$("#use_card_count").html(json.data.count);
					$("#use_card_total_balance").html(json.data.totalBalance);
					
					$.each(json.data.list, function (i) {
						var html = $("<tr id=\"use_card_item_" + this.cardNo + "\"><td>" + this.cardNo + "</td><td>" + this.balance + "</td><td>" + this.useAmount + "</td><td>" + this.useBalance + "</td><td>[ <a href=\"javascript:;\" onclick=\"delUseCard('" + this.cardNo + "');return false;\">取消</a> ]</td></tr>");
						$("#use_card_list tr:last").after(html);						
					});
					
				}
			}
		}
	});
}

function getBindCardList(){
	$.ajax( {
		cache : false,
		type : "get",
		url : "/neworder/getBindCardList.jspa",
		dataType : 'json',
		success : function(json) {			
			if (json){
				if(json.flag==0){
					alert(json.msg);
				}else{					
					$("#bind_card_list tr:gt(0)").remove();
					
					$.each(json.data.list, function (i) {
						var html = $("<tr><td><input type=\"checkbox\" id=\"ckb_bind_card_" + this.cardNo + "\" value=\"" + this.cardNo + "\" />&nbsp;<label for=\"ckb_bind_card_" + this.cardNo + "\">" + this.cardNo + "</label></td><td>" + this.parValue + "</td><td>" + this.balance + "</td><td>" + this.expiredDate + "</td></tr>");
						$("#bind_card_list tr:last").after(html);						
					});
					
				}
			}
		}
	});
}
